package com.gvitz.transaction.activities;



import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gvitz.transaction.R;
import com.gvitz.transaction.utils.crouton.Crouton;
import com.gvitz.transaction.utils.crouton.Style;
import com.gvitz.transaction.utils.validations.Field;
import com.gvitz.transaction.utils.validations.Form;
import com.gvitz.transaction.utils.validations.NotEmpty;


public class NewPingActivity extends GVActivity  {
 
	    private EditText mPass1;
	    private EditText mPass2;
	    
	    private Button mSubmit;

	    // Form used for validation
	    private Form mForm;
	    private boolean flag = true;

	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_newping);
	        initFields();
	        initValidationForm();
	        initCallbacks();
		
	    }


	    private void initFields() {
	    	
	    	//inicializar variables
	    	
	        mPass1 = (EditText) findViewById(R.id.pass1_pin);
	        mPass2 = (EditText) findViewById(R.id.pass2_pin);
	        mSubmit = (Button) findViewById(R.id.submit_pin);	  
	        
	        Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
		      
	           TextView t = (TextView) findViewById(R.id.icon_new_pin);
		       TextView td = (TextView) findViewById(R.id.icon_new_pin_1);

		       t.setTypeface(font);
		       td.setTypeface(font);
	    }

	    private void initValidationForm() {
	    	
	    	//inicializar form
	        mForm = new Form(this);
	        mForm.addField(Field.using(mPass1).validate(NotEmpty.build(this)));
	        mForm.addField(Field.using(mPass2).validate(NotEmpty.build(this)));

	    }

	    private void initCallbacks() {

	        mSubmit.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View view) {
	                submit();
	            }
	        });
	    }

	    private void submit() {
	    	
	    	// revisar form
	        if (mForm.isValid()) {
	        	
	        	//ver si los PIN son iguales
	        	if((mPass1.length() == 4) && (mPass2.length() == 4)){
	        	
	        		if(mPass1.getText().toString().equals(mPass2.getText().toString())){
	        			
	        	   register_success(); 	  
	        	   
		        	}
		        	else{
		        		
		        		Crouton.makeText(this, getString(R.string.zvalidations_not_password), Style.ALERT).show();
		        		
		        	}
		        }else{
		        	
		        	Crouton.makeText(this, getString(R.string.zvalidations_not_pin), Style.ALERT).show();
		        	
		        	}
	        }
	    }
	    
		@Override
		public void onBackPressed() {
			return;
		}
	    
	    public void register_success(){
	    	    	
	    	new Handler().postDelayed(new Runnable() {
					
					  @Override
			          public void run() {
					 		
			        		SharedPreferences prefs = getSharedPreferences("preferencias",Context.MODE_PRIVATE);
					 		SharedPreferences.Editor editor = prefs.edit();
					 		editor.putString("pin_usuario",mPass1.getText().toString());
					 		editor.putString("first_time","2");
					 		editor.commit();
					 		flag = false;
					 		
						    Toast.makeText(getApplicationContext(),R.string.creado_pin, Toast.LENGTH_LONG).show();
						 	Intent i=new Intent(NewPingActivity.this, PingActivity.class);
						 	i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
							startActivity(i); 
							overridePendingTransition (R.anim.open_next, R.anim.close_main);
							finish();
						
			          }
			      }, 400);
	    	
	 	    
	    }
	    
	    @Override 
		public void onStop() {

	    	if(flag){
				SharedPreferences prefs = getSharedPreferences("preferencias",Context.MODE_PRIVATE);
		 		SharedPreferences.Editor editor = prefs.edit();
		 		editor.putString("first_time","1");
		 		editor.commit();
    		}
            super. onStop();
	     } 
	}
         
   	